var app = getApp()
Page({
  data: {
    goods:[],
    oldGoods:[],
    payTypePicker:true,
    payTypeChecked:true,
    goodsPriceShow:true,
    actualPriceShow:true,
    goodsInfoShow:true,
    newTotalPrice:0,
    oldTotalPrice:0,
    orderNo:'',
    orderTime:'',
    saler:'',
    orderType:'',
    accountId:'',
    orderId:'',
    voucher:false,
    payCode:'',
    isScanCode:false
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        accountId:res.data.id
      })
    })
    let orderId = options.orderId
    // let orderId = 11
    let type = options.type  //0 正常付款  1代付款详情  2已付款详情
    if (type==2) {
      wx.setNavigationBarTitle({
        title: '订单详情'
      })
    }
    this.setData({
      orderId,
      orderType:type,
    })
    this.getOrderDetail(orderId)
    this.monitorVoucher(orderId)
    if (type!=2) {
      this.getPayCode(orderId)
    }
  },
  onShow: function () {

  },
  //打开选择器
  bindOpenPicker(){
    this.setData({
      payTypePicker:false
    })
  },
  cancel(){
    this.setData({
      payTypePicker:true
    })
  },
  confirm(){
    this.setData({
      payTypePicker:true
    })
  },
  //请选择微信支付
  bindChoseWeiPay(){
    this.setData({
      payTypeChecked:true
    })
  },
  //选择线下支付
  bindChoseCashPay(){
    this.setData({
      payTypeChecked:false
    })
  },
  getOrderDetail(orderId){
    wx.showLoading({
      title:'正在加载'
    })
    let that = this
    let data = {
      orderId
    }
    console.log(data)
    app.request('wx_app/user_order/detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let goods = [],
        oldGoods = []
        res.data.orderDetails.forEach(item=>{
          goods.push({
            url:item.pic,
            name:item.name,
            price:item.price,
            actualPrice:item.actualPrice,
            num:item.amount,
            type:item.des,
            id:item.productId,
            color:item.tagColor,
            companyName:item.productTag
          })
        })
        res.data.oldSkus.forEach(item=>{        
          oldGoods.push({
            url:item.mainImgUrl,
            name:item.productName,
            price:item.stockPrice,
            num:item.amount,
            type:item.versionValues,
            imgs:[item.imgUrl2,item.imgUrl3,item.imgUrl4,item.imgUrl5],
            color:item.tagColor,
            companyName:item.productTag,
            direct:item.status==0?"入库":"废弃",
            discardPrice:item.discardPrice
          })
        })
        that.setData({
          goods,
          oldGoods,
          payWay:res.data.payWay,
          orderNo:res.data.orderNo,
          orderTime:res.data.createTime,
          saler:res.data.accountName
        })
        that.totalPrice()
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  totalPrice(){
    let newTotalPrice = 0,
    oldTotalPrice = 0
    this.data.goods.forEach(item=>{
      newTotalPrice += item.actualPrice*item.num
    })
    this.data.oldGoods.forEach(item=>{
      oldTotalPrice += item.price*item.num
    })
    this.setData({
      newTotalPrice,
      oldTotalPrice
    })
  },
  bindToPayMoney(){
    var that = this
    let data = {
      orderId:that.data.orderId
    }
    wx.showModal({
      title: '确认线下结算',
      content: '确定客户已经结算了吗?',
      success: function(res) {
        if (res.confirm) {
          app.request('wx_app/pay/underLine/finish',data,'GET').then(res=>{
            console.log(res)
            if (res.code==1) {
              wx.switchTab({
                url:'../my'
              })
            }else{
              app.errorMsg(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
          app.request('wx_app/pay/underLine/cancel',data,'GET').then(res=>{
            console.log(res)
            
          })
        }
      }
    })
  },
  getPayCode(orderId){
    var that = this
    let data = {
      orderId
    }
    console.log(data)
    app.request('wx_app/pay/qr_url',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.setData({
          payCode:res.data
        })
        // let price = this.data.newTotalPrice - this.data.oldTotalPrice
        // wx.navigateTo({
        //   url:'../../stock/scanCode?url='+res.data+'&price='+price
        // })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  toPay(){
    this.setData({
      orderType:0
    })
  },
    //删除订单
  deleteOrderTap(){
    let data = {
      orderId:this.data.orderId,
      accountId:this.data.accountId
    }
    console.log(data)
    wx.showModal({
      title: '删除订单',
      content: '确定要删除订单吗?',
      cancelColor:"#333333",
      confirmColor: "#539FFF",
      success: function (res) {
        if (res.confirm) { 
          app.request('wx_app/user_order/del',data,'GET').then(res=>{              
            console.log(res)
            if (res.code==1) {                
              wx.showToast({
                title:'删除成功',
                success(){
                  setTimeout(function(){
                    wx.navigateBack()
                  },1000)
                }
              })
            }else{
              app.errorMsg(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //填写凭证信息
  bindVoucher(){
    wx.navigateTo({
      url:'../orderDetail/voucher?orderId='+this.data.orderId
    })
    
  },
  //查看凭证信息
  checkVoucher(){
    wx.navigateTo({
      url:'../orderDetail/checkVoucher?orderId='+this.data.orderId
    })
  },
  //检测是否有凭证
  monitorVoucher(orderId){
    let that = this
    let data = {
      orderId
    }
    app.request('wx_app/user_order/getVoucher',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (res.data.buyerName===null||res.data.buyerPhone===null) {
          that.setData({
            voucher:false
          })
        }else{
          that.setData({
            voucher:true
          })
        }
      }
    })
  },
  bindLookCode(){
    let that = this
    this.setData({
      isScanCode:true
    })
    wx.getUserInfo({
      success(res){
        console.log(res.userInfo)
        that.setData({
          nickName:res.userInfo.nickName,
          avatarUrl:res.userInfo.avatarUrl
        })
      }
    })
  },
  quit(){
    this.setData({
      isScanCode:false
    })
  }
})